package com.soup.lab.http;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

@SpringBootTest
public class Client {

    @Test
    public void test() {
        RestTemplate restTemplate = new RestTemplate();
        String ret = restTemplate.getForObject("http://127.0.0.1:8080/hello", String.class);
        System.out.println(ret);
    }

    @Test
    public void test2() throws IOException {
        URL url = new URL("http://192.168.43.144:8080/hello");
        URLConnection connection = url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()))) {
            String s = reader.readLine();
            System.out.println(s);
        }
    }

    @Autowired
    @Qualifier("insecureRestTemplate")
    RestTemplate insecureRestTemplate;

    @Test
    public void testHttpsSkip() {
        String object = insecureRestTemplate.getForObject("https://wxapp-hzfc-cn/fgfw/wymjapitest/wymjapi/login?username=9133011073030177X6&password=d3ltajExMTIwMA==", String.class);
        System.out.println(object);
    }

    @Autowired
    RestTemplate restTemplate;

    @Test
    public void testHttps() {
        String object = restTemplate.getForObject("https://wxapp-hzfc-cn/fgfw/wymjapitest/wymjapi/login?username=9133011073030177X6&password=d3ltajExMTIwMA==", String.class);
        System.out.println(object);
    }

}
